# import time
# from datetime import datetime

from appium import webdriver
# from appium.webdriver.common.appiumby import AppiumBy
# from appium.webdriver.common.touch_action import TouchAction
# from selenium.webdriver.ie.webdriver import WebDriver
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.wait import WebDriverWait
# from sympy.physics.units import current, electronvolt
# from urllib3.util import current_time


class CommonDriver:
    def get_driver(self):
        desc = {
            'platformName': 'Android',
            'platformVersion': '7',
            'appPackage': 'com.dangdang.buy2',
            # 'appPackage': 'com.microvirt.launcher2',
            'appActivity': 'com.dangdang.buy2.StartupActivity',
            # 'appActivity': 'com.microvirt.launcher.Launcher',
            'noReset': False,
            'unicodeKeyboard': True,
            'udid': '127.0.0.1:21513',
        }

        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    # driver.find_element(AppiumBy.ID,'com.dangdang.buy2:id/tv_sure').click()
    # driver.find_elements(AppiumBy.CLASS_NAME,'android.widget.TextView')[3].click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@text="同意"]').click()
    # driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@text="同意"]').click()
    # driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[contains(@text,"不同")]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure" and @text="同意"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]/parent::android.widget.LinearLayout').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/research_flipper_textview"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys('浪潮之巅')
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').clear()
    # time.sleep(2)
    # driver.quit()

    # try:
    #     screen_size = driver.get_window_size()
    #     width = screen_size["width"]
    #     print("-----------------", width)
    #     height = screen_size["height"]
    #
    #     start_x = width / 2
    #     start_y = height * 0.8
    #     end_x = width / 2
    #     end_y = height * 0.2
    #     print(start_x, start_y, end_x, end_y)
    #
    #     driver.swipe(start_x, start_y, end_x, end_y)
    #     time.sleep(2)
    # finally:
    #     driver.quit()
    # driver.tap([(59,419)],500)
    # dangdang_icon=driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@text="当当"]')
    # action=TouchAction(driver)
    # action.long_press(dangdang_icon).move_to(x=105,y=734).release().perform()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # current_time=datetime.now()
    # print('当前时间',current_time)
    # try:
    #     element=WebDriverWait(driver,20).until(
    #         EC.visibility_of_element_located(
    #             (AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sur"]') )
    #     )
    # finally:
    #     current_time = datetime.now()
    #     print('当前时间', current_time)
